package com.jichangxiu.framework.factory;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.jichangxiu.common.entity.bo.Record;
import com.jichangxiu.common.utils.IpUtils;
import com.jichangxiu.framework.service.LogService;
import com.jichangxiu.framework.service.LoginInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.TimerTask;

/**
 * AsyncFactory:
 *
 * @author Jcx
 * @create 2023-04-06 15:56
 */
public class AsyncFactory {

    private static final Logger async_task_logger = LoggerFactory.getLogger("async-task");

    /**
     * 操作日志记录
     *
     * @return 任务task
     */
    public static TimerTask recordPlatformLog(LogService logService, Record record) {
        return new TimerTask() {
            @Override
            public void run() {
                // 插入数据
                if (ObjectUtil.isNotEmpty(logService) && ObjectUtil.isNotEmpty(record)) {
                    String ip = record.getStr("ip");
                    if (StrUtil.isNotEmpty(ip)) {
                        record.put("location", IpUtils.getRealAddress(ip));
                    }
                    logService.saveLog(record);
                }
            }
        };
    }

    /**
     * 记录登录信息
     *
     * @return 任务task
     */
    public static TimerTask recordPlatformLoginInfo(LoginInfoService loginInfoService, Record record) {
        return new TimerTask() {
            @Override
            public void run() {
                // 插入数据
                if (ObjectUtil.isNotEmpty(loginInfoService) && ObjectUtil.isNotEmpty(record)) {
                    String ip = record.getStr("ip");
                    if (StrUtil.isNotEmpty(ip)) {
                        record.put("location", IpUtils.getRealAddress(ip));
                    }
                    loginInfoService.saveLoginInfo(record);
                }
            }
        };
    }

}
